﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class MixRowHandle : MonoBehaviour
{
    public static MixRowHandle Instance;

    //public DrawSprite drawStatic;
    //DrawSprite drawDynamic;
    [SerializeField]
    private List<DrawSprite> drawer = new List<DrawSprite>();

    public readonly Dictionary<int, Dictionary<string, SpriteInfoGroup>> IndexSpriteInfo = new Dictionary<int, Dictionary<string, SpriteInfoGroup>>();

    private Dictionary<int, Dictionary<int, MeshInfo>> graphicMeshInfo = new Dictionary<int, Dictionary<int, MeshInfo>>();


    bool update = false;

    int updateID = -1;


    void OnEnable()
    {
        Instance = this;
        Initialize();
    }
    private void Awake()
    {
        //Instance = this;
        Initialize();
    }
    public void Initialize()
    {
        // SpriteAssets mSpriteAsset = drawStatic.m_spriteAsset;
        for (int i = 0; i < drawer.Count; i++)
        {
            SpriteAssets mSpriteAsset = drawer[i].m_spriteAsset;
            if (!IndexSpriteInfo.ContainsKey(mSpriteAsset.Id))
            {
                Dictionary<string, SpriteInfoGroup> spriteGroup = new Dictionary<string, SpriteInfoGroup>();
                foreach (var item in mSpriteAsset.groupList)
                {
                    if (!spriteGroup.ContainsKey(item.Tag) && item.ListSpriteInfos != null && item.ListSpriteInfos.Count > 0)
                        spriteGroup.Add(item.Tag, item);
                }
                IndexSpriteInfo.Add(mSpriteAsset.Id, spriteGroup);
            }
        }


    }

    private void Update()
    {
        if (update)
        {
            for (int i = 0; i < drawer.Count; i++)
            {
                int ID = drawer[i].m_spriteAsset.Id;
                if (!graphicMeshInfo.ContainsKey(ID))
                {
                    drawer[ID].MeshInfo = null;
                    return;
                }
                Dictionary<int, MeshInfo> textMeshInfo = graphicMeshInfo[ID];
                if (textMeshInfo == null || textMeshInfo.Count == 0)
                    drawer[ID].MeshInfo = null;
                else
                {
                    MeshInfo meshInfo = Pool<MeshInfo>.Get();
                    meshInfo.Clear();
                    foreach (var item in textMeshInfo)
                    {
                        meshInfo.Vertices.AddRange(item.Value.Vertices);
                        meshInfo.UVs.AddRange(item.Value.UVs);
                    }
                    if (drawer[ID].MeshInfo != null)
                        Pool<MeshInfo>.Release(drawer[ID].MeshInfo);

                    drawer[ID].MeshInfo = meshInfo;
                }
            }

            update = false;
        }
    }
    public void UpdateMeshInfo(int instanceId, int id, List<SpriteVertInfo> vertList)
    {
        updateID = id;
        //textDrawer.GetInstanceID();
        Dictionary<int, MeshInfo> textMeshInfo;
        if (!graphicMeshInfo.TryGetValue(id, out textMeshInfo))
        {
            textMeshInfo = new Dictionary<int, MeshInfo>();
            graphicMeshInfo.Add(id, textMeshInfo);
        }
        MeshInfo meshInfo;
        if (!textMeshInfo.TryGetValue(instanceId, out meshInfo))
        {
            meshInfo = Pool<MeshInfo>.Get();
            textMeshInfo.Add(instanceId, meshInfo);
        }
        meshInfo.Clear();
        for (int i = 0; i < vertList.Count; i++)
        {
            for (int j = 0; j < vertList[i].Pos.Length; j++)
            {
                //世界转本地坐标->避免位置变换的错位
                meshInfo.Vertices.Add(Utility.TransformWorld2Point(drawer[updateID].transform, vertList[i].Pos[j]));
            }
            meshInfo.UVs.AddRange(vertList[i].UVs);
        }
        update = true;
    }


    public void ClearDrawData(int instanceId)
    {
        Dictionary<int, MeshInfo> temp_Dic;
        if (graphicMeshInfo.TryGetValue(instanceId, out temp_Dic))
        {
            temp_Dic.Clear();
        }
    }
}

#region 模型数据信息
public class MeshInfo
{
    public List<Vector3> Vertices = ListPool<Vector3>.Get();
    public List<Vector2> UVs = ListPool<Vector2>.Get();
    public List<Color> Colors = ListPool<Color>.Get();
    public List<int> Triangles = ListPool<int>.Get();

    public void Clear()
    {
        Vertices.Clear();
        UVs.Clear();
        Colors.Clear();
        Triangles.Clear();
    }

    public void Release()
    {
        ListPool<Vector3>.Release(Vertices);
        ListPool<Vector2>.Release(UVs);
        ListPool<Color>.Release(Colors);
        ListPool<int>.Release(Triangles);

        Pool<MeshInfo>.Release(this);
    }

}
#endregion
